﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using Aspose.Slides;
using System.Drawing;
using System.IO;

namespace Meeting.Office2Image
{
    public class PPT2Image
    {
        public static string errorString = "";

        /// <summary>
        ///  ppt 转图片
        /// </summary>
        /// <param name="inputPath"></param>
        /// <param name="outputPath"></param>
        /// <param name="imageName"></param>
        /// <param name="startPageNum"></param>
        /// <param name="endPageNum"></param>
        /// <param name="imageFormat"></param>
        /// <param name="imageCount"></param>
        /// <returns></returns>
        public static bool ConvertPptToImage(string inputPath, string outputPath, string imageName,
                            int startPageNum, int endPageNum, ImageFormat imageFormat, Size imgSize, out int imageCount)
        { 
            imageCount = 0;
            try
            {
                int iCount = 0;
                Presentation pres = new Presentation(inputPath);//加载文件
                //
                startPageNum = 1;
                endPageNum = pres.Slides.Count;
                //
                for (int j = startPageNum; j <= endPageNum; j++)
                {
                    try
                    {
                        Slide slide = pres.GetSlideByPosition(j);
                        System.Drawing.Image image = slide.GetThumbnail(imgSize);
                        iCount++;
                        image.Save(Path.Combine(outputPath, imageName) + (imageName.Length == 0 ? "" : "_") + (j).ToString() + "." + imageFormat.ToString(), imageFormat);
                    }
                    catch { }
                } 
                imageCount = iCount;
                return true;
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return false;
            }
        }
    }
}
